package com.xiangxiao.rpan.storage.download.local;

import com.xiangxiao.rpan.storage.core.DownloadFileTool;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/12/1 17:41
 */
@Component(value = "downloadFileLocal")
public class DownloadFileLocal implements DownloadFileTool {
  @Override
  public void readStreamToFile(String path, HttpServletResponse response) throws FileNotFoundException {
    // 下载本地文件
    File file = new File(path);
    String filename = file.getName(); // 文件的默认保存名
    // 读到流中
    InputStream inStream = new FileInputStream(path);// 文件的存放路径
    // 设置输出的格式
    response.reset();
    response.setContentType("bin");
    response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    // 循环取出流中的数据
    byte[] b = new byte[100];
    int len;
    try {
      while ((len = inStream.read(b)) > 0)
        response.getOutputStream().write(b, 0, len);
      inStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @Override
  public String convertMuitlFileToStream(MultipartFile multipartFile, Integer chunkNumber, Integer totalChunks, long blockSize) {
    // todo
    return null;
  }
}
